﻿
@{
    ViewBag.Title = "PEMaterialFeeView";
    Layout = "~/Views/Shared/_MyLayout.cshtml";
}

<fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px;">
    <legend>材料成本</legend>
</fieldset>
<form class="layui-form" enctype="multipart/form-data">
    <div class="layui-form-item">
        <label class="layui-form-label">搜索</label>
        <div class="layui-input-block">
            <input type="text" id="SearchText" name="Title1" required lay-verify="required" onkeypress="getKey()" placeholder="输入安装项目名称" class="layui-input" style="width:300px;float:left;">
            <input type="text" value="" style="display:none" />
            <button type="button" class="layui-btn layui-btn-normal" style="float:left;" onclick="SearchFunc()"><i class="layui-icon">&#xe615;</i></button>
            <button type="button" class="layui-btn layui-btn-danger" style="float:left;" id="BtnAdd"><i class="layui-icon">&#xe61f;</i>新增引入</button>
        </div>
    </div>
</form>

<div id="QuoteMaterialFeeLayer" style="display:none;">
    <form class="layui-form" enctype="multipart/form-data">
        <div class="layui-form-item">
            <label class="layui-form-label">搜索</label>
            <div class="layui-input-block">
                <input type="text" id="QuoteSearchText" name="Title1" required lay-verify="required" onkeypress="QuoteGetKey()" placeholder="输入安装项目名称或规格型号" class="layui-input" style="width:300px;float:left;">
                <input type="text" value="" style="display:none" />
                <button type="button" class="layui-btn layui-btn-normal" style="float:left;" onclick="QuoteSearchFunc()"><i class="layui-icon">&#xe615;</i></button>
                <button type="button" class="layui-btn layui-btn-danger" style="float:left;" id="QuoteBtnAdd"><i class="layui-icon">&#xe61f;</i>引入选中项</button>
                <button type="button" class="layui-btn" style="float:left;" id="QuoteBtnAddNewEquip"><i class="layui-icon">&#xe61f;</i>添加临时数据</button>
            </div>
        </div>
    </form>
    <table id="QuoteMaterialFeeTable" lay-filter="QuoteMaterialFeeTablefilter"></table>
</div>
<div id="QuoteMaterialPriceSingle" style="display:none">
    <table id="QuoteMaterialPriceSingleTable" lay-filter="QuoteMaterialPriceSinglefilter"></table>
</div>


<div id="AddNewEquipLayer" style="display:none">
    <form class="layui-form" id="AddNewEquipForm">
        <div class="layui-form-item" style="margin-top:10px;">
            <label class="layui-form-label">材料名称</label>
            <div class="layui-input-inline" style="width:150px;">
                <input type="text" name="EquipName" value="" required lay-verify="required" placeholder="请输入材料名称" autocomplete="off" class="layui-input">
            </div>
            <label class="layui-form-label">规格型号</label>
            <div class="layui-input-inline" style="width:200px;">
                <input type="text" name="Specification" required value="" lay-verify="required" placeholder="请输入规格型号" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">类型</label>
            <div class="layui-input-inline" style="width:200px;">
                <select name="Type">
                    <option value="电缆">电缆</option>
                    <option value="电缆附件">电缆附件</option>
                    <option value="设备">设备</option>
                    <option value="架空">架空</option>
                </select>
            </div>
            <label class="layui-form-label">单位</label>
            <div class="layui-input-inline" style="width:50px;">
                <input type="text" name="Unit" value="" placeholder="" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label" style="width:30px;">价格</label>
            <div class="layui-input-inline" style="width:120px;">
                <input type="number" name="QuoteMoney" value="" required lay-verify="required" placeholder="" autocomplete="off" class="layui-input">
            </div>
            <label class="layui-form-label" style="width:30px;">数量</label>
            <div class="layui-input-inline" style="width:120px;">
                <input type="number" name="Quantity" value="" required lay-verify="required" placeholder="" autocomplete="off" class="layui-input">
            </div>
            <div class="layui-input-inline" style="width:100px;">
                <button type="submit" id="PreLayerSubmit" class="layui-btn" lay-submit lay-filter="AddNewEquipFormVerify" style="display:none;">立即提交</button>
                <button type="button" class="layui-btn" style="float:left;" id="AddNewEquipFormSave"><i class="layui-icon">&#xe621;</i>保存</button>
                @*<button type="button" class="layui-btn" style="float:left;" id="Debug"><i class="layui-icon">&#xe621;</i>Debug</button>*@
            </div>
        </div>
    </form>
</div>


<div class="layui-tab" lay-filter="tabFilter">
    <ul class="layui-tab-title">
        <li class="layui-this">外线</li>
        <li>开闭所部分</li>
        <li>公配部分</li>
        <li>专配部分</li>
        <li>运行准备</li>
    </ul>
    <div class="layui-tab-content">
        <div class="layui-tab-item layui-show">
            <table id="WX_MaterialFeeTable" lay-filter="WX_MaterialFeeTablefilter"></table>
        </div>
        <div class="layui-tab-item">
            <table id="KBS_MaterialFeeTable" lay-filter="KBS_MaterialFeeTablefilter"></table>
        </div>
        <div class="layui-tab-item">
            <table id="GP_MaterialFeeTable" lay-filter="GP_MaterialFeeTablefilter"></table>
        </div>
        <div class="layui-tab-item">
            <table id="ZP_MaterialFeeTable" lay-filter="ZP_MaterialFeeTablefilter"></table>
        </div>
        <div class="layui-tab-item">
            <table id="YXZB_MaterialFeeTable" lay-filter="YXZB_MaterialFeeTablefilter"></table>
        </div>
    </div>
</div>

<script>
    var table;
    var TabIndex = 0;
    var PreEstId = parent.$("#PreEstId").val();
    var PreEstType = parent.$("#ProjectType").val();
    //注意：选项卡 依赖 element 模块，否则无法进行功能性操作
    layui.use(['form', 'table', 'upload', 'layer', 'element'], function () {
        var element = layui.element;
        var upload = layui.upload;
        table = layui.table;
        var tab = parent.tab;
        var layer = layui.layer;
        var form = layui.form;

        var IsValidate = 0;
        var LayerIndex;
        var LayerTempIndex;



        form.on('submit(AddNewEquipFormVerify)', function (data) {
            IsValidate = 1;
            return false;
        });

        $("#AddNewEquipFormSave").on("click", function () {
            //首先验证输入框正确性
            $("#PreLayerSubmit").click();
            if (IsValidate != 1) return;
            IsValidate = 0;
            var obj = $("#AddNewEquipForm").serializeArray();
            var data = {};
            $.each(obj, function (i, v) {
                data[v.name] = v.value;
            });
            data["PreEstId"] = PreEstId;
            data["PreEstType"] = PreEstType;
            data["AvgPrice"] = 0;
            data["Code"] = "";
            data["EquipPriceId"] = 0;
            data["Price"] = 0;
            data["NormalName"] = "";
            data["NormalPrice"] = "null";
            data["NormalSpeci"] = "";
            data["TypeL1"] = "";
            if (TabIndex == 0) {
                data["TabType"] = "WX";
            }
            else if (TabIndex == 1) {
                data["TabType"] = "KBS";
            }
            else if (TabIndex == 2) {
                data["TabType"] = "GP";
            }
            else if (TabIndex == 3) {
                data["TabType"] = "ZP";
            }
            else if (TabIndex == 4) {
                data["TabType"] = "YXZB";
            }
            //临时情况
            var dataArray = [];
            dataArray.push(data);

            $.ajax({
                type: "POST",
                url: "/Estimate/MaterialFeeAdd",
                async: false,
                contentType: "application/json",
                dataType: "json",
                data: JSON.stringify(dataArray),
                success: function (msg) {
                    if (msg.result == "OK") {
                        parent.layer.msg('新增成功,请增加详细信息', { icon: 6, time: 1000 });
                        if (TabIndex == 0) {
                            table.reload('WX_MaterialFeeTable');
                        }
                        else if (TabIndex == 1) {
                            table.reload('KBS_MaterialFeeTable');
                        }
                        else if (TabIndex == 2) {
                            table.reload('GP_MaterialFeeTable');
                        }
                        else if (TabIndex == 3) {
                            table.reload('ZP_MaterialFeeTable');
                        }
                        else if (TabIndex == 4) {
                            table.reload('YXZB_MaterialFeeTable');
                        }
                        layer.close(LayerTempIndex);
                    } else {
                        alert(msg.result);
                    }
                }
            });


        });





        $("#QuoteBtnAddNewEquip").on("click", function () {
            LayerTempIndex = layer.open({
                type: 1,
                title: ['添加临时数据', 'font-size:18px;'],
                area: ['700px', '400px'],
                offset: 'rt',
                content: $("#AddNewEquipLayer"),  //这里content是一个DOM，注意：最好该元素要存放在body最外层，否则可能被其它的相对元素所影响
                success: function (layero, index) {
                    //$("#EditFrom input[name='TestFeeId']").val("");
                }
            });
        });




        //监听选项卡切换
        element.on('tab(tabFilter)', function (data) {
            //index从0开始
            TabIndex = data.index;
        });

        $("#BtnAdd").on("click", function () {
            layerindex = layer.open({
                type: 1,
                title: ['材料成本基础数据', 'font-size:18px;'],
                area: '1020px',
                offset: 'rt',
                content: $("#QuoteMaterialFeeLayer"),  //这里content是一个DOM，注意：最好该元素要存放在body最外层，否则可能被其它的相对元素所影响
                success: function (layero, index) {
                    //$("#EditFrom input[name='TestFeeId']").val("");
                }
            });
        });


        table.render({
            elem: '#QuoteMaterialFeeTable'
            , method: 'post'
            //, height: 315
            , url: '/Estimate/QuoteEquipTableRequest' //数据接口
            , page: true //开启分页
            , where: { SortName: "EquipPriceId", Order: "Desc", Condition: " and IsDelete<>1 " }
            , limits: [10, 50, 100]
            , cols: [[ //表头
                { type: 'checkbox' }
                , { field: 'Code', title: '编号', width: 100 }
                //, { field: 'EquipName', title: '名称', width: 150 }
                //, { field: 'Specification', title: '规格型号', width: 250 }
                , { field: 'NormalName', title: '一般名称', width: 150 }
                , { field: 'NormalSpeci', title: '统一型号', width: 200 }
                , { field: 'Unit', title: '单位', width: 70 }
                , { field: 'Type', title: '类型', width: 100 }
                , { field: 'NormalPrice', title: '参考单价', width: 100 }
                , { field: 'QuoteMoney', title: '引入单价', width: 100, edit: 'text' }
                , { field: 'Quantity', title: '数量', width: 80, edit: 'text' }

            ]]
        });

        table.on('edit(QuoteMaterialFeeTablefilter)', function (obj) {
            var data = obj.data;
            var field = obj.field;
            try {
                if (field == "QuoteMoney") {
                    eval(data.QuoteMoney);
                }
                if (field == "Quantity") {
                    eval(data.Quantity);
                }
            } catch (e) {
                layer.msg('数据填写格式有误', { icon: 2, time: 1500 });
                table.reload('QuoteMaterialFeeTable');
                return;
            }
        });

        //监听行双击事件
        table.on('rowDouble(QuoteMaterialFeeTablefilter)', function (obj) {
            //obj 同上
            layerindex = layer.open({
                type: 1,
                title: ['历史购买信息', 'font-size:18px;'],
                area: '920px',
                offset: 'auto',
                content: $("#QuoteMaterialPriceSingle"),  //这里content是一个DOM，注意：最好该元素要存放在body最外层，否则可能被其它的相对元素所影响
                success: function (layero, index) {
                    //$("#EditFrom input[name='TestFeeId']").val("");
                }
            });

            table.render({
                elem: '#QuoteMaterialPriceSingleTable'
                , method: 'post'
                //, height: 315
                , url: '/Estimate/QuoteEquipPriceSingleTableRequest' //数据接口
                , page: true //开启分页
                , where: { SortName: "id", Order: "Desc", Condition: " and EquipPriceId=" + obj.data.EquipPriceId }
                , limits: [10, 50, 100]
                , cols: [[ //表头
                     { field: 'SellerName', title: '供应商名称', width: 150 }
                    , { field: 'Price', title: '价格', width: 100 }
                    , { field: 'ProjectName', title: '项目名称', width: 250 }
                    , { field: 'Buytime', title: '购买时间', width: 150 }
                ]]
            });

        });

        $("#QuoteBtnAdd").on("click", function () {
            var checkStatus = table.checkStatus('QuoteMaterialFeeTable');
            var data = checkStatus.data;    //这里得到的data是array类型
            if (checkStatus.data.length == 0) { return; }
            //alert(JSON.stringify(data));
            //先检查内容
            data.forEach(function (item, index) {
                item["PreEstId"] = PreEstId;
                item["PreEstType"] = PreEstType;
                if (TabIndex == 0) {
                    item["TabType"] = "WX";
                }
                else if (TabIndex == 1) {
                    item["TabType"] = "KBS";
                }
                else if (TabIndex == 2) {
                    item["TabType"] = "GP";
                }
                else if (TabIndex == 3) {
                    item["TabType"] = "ZP";
                }
                else if (TabIndex == 4) {
                    item["TabType"] = "YXZB";
                }
            });

            $.ajax({
                type: "POST",
                url: "/Estimate/MaterialFeeAdd",
                async: false,
                contentType: "application/json",
                dataType: "json",
                data: JSON.stringify(data),
                success: function (msg) {
                    if (msg.result == "OK") {
                        layer.msg('引入成功', { icon: 6, time: 1000 });
                        if (TabIndex == 0) {
                            table.reload('WX_MaterialFeeTable');
                        }
                        else if (TabIndex == 1) {
                            table.reload('KBS_MaterialFeeTable');
                        }
                        else if (TabIndex == 2) {
                            table.reload('GP_MaterialFeeTable');
                        }
                        else if (TabIndex == 3) {
                            table.reload('ZP_MaterialFeeTable');
                        }
                        else if (TabIndex == 4) {
                            table.reload('YXZB_MaterialFeeTable');
                        }

                    } else {
                        alert(msg.result);
                    }
                }
            });
        });
        //////////////////////////////////////////////////////////////////////

        //外线表格部分
        table.render({
            elem: '#WX_MaterialFeeTable'
            , method: 'post'
            //, height: 315
            , url: '/Estimate/PEMaterialTableRequest' //数据接口
            , page: true //开启分页
            , where: { SortName: "PEMaterialPriceId", Order: "Desc", Condition: " and TabType='WX' and IsDelete<>1 and PreEstId=" + PreEstId + " and PreEstType='" + PreEstType + "'" } 
            , limits: [10, 50, 100]
            , cols: [[ //表头
                { field: 'Code', title: '编号', width: 100}
                , { field: 'EquipName', title: '名称', width: 250, edit: 'text'}
                , { field: 'Specification', title: '规格型号', width: 250, edit: 'text'}
                , { field: 'Unit', title: '单位', width: 70 }
                , { field: 'DesignQuantity', title: '图纸量', width: 80, edit: 'text' }
                , { field: 'Quantity', title: '数量', width: 80, edit: 'text' }
                , { field: 'QuoteMoney', title: '单价', width: 100, edit: 'text' }
                , {
                    title: '合价', width: 120,
                    templet: function (d) {
                        return (d.Quantity * d.QuoteMoney).toFixed(2);
                    }
                }
                , {
                    title: '单价对比', width: 100,
                    templet: function (d) {
                        if (d.NormalPrice == null) {
                            return '无对比';
                        }
                        else if (d.QuoteMoney > d.NormalPrice) {
                            return '<i class="layui-icon layui-icon-up" style="font-size:16px;color:#FF0000;"></i>' + (d.QuoteMoney - d.NormalPrice).toFixed(0);
                        }
                        else if (d.QuoteMoney < d.NormalPrice) {
                            return '<i class="layui-icon layui-icon-down" style="font-size: 16px; color: #238E23;"></i>' + (d.NormalPrice - d.QuoteMoney).toFixed(0);
                        }
                        else {
                            return '--';
                        }
                    }
                }
                , {
                    field: "operation", title: "操作", width: 80, fixed: 'right',
                    templet: function (d) {
                        return '<a class="layui-btn layui-btn-xs" lay-event="TableDel">删除</a>';
                    }
                }

            ]]
        });

        table.on('edit(WX_MaterialFeeTablefilter)', function (obj) { //注：edit是固定事件名，test是table原始容器的属性 lay-filter="对应的值"
            //console.log(obj.value); //得到修改后的值
            //console.log(obj.field); //当前编辑的字段名
            //console.log(obj.data); //所在行的所有相关数据  
            var data = obj.data;
            var field = obj.field;
            try {
                if (field == "QuoteMoney") {
                    eval(data.QuoteMoney);
                }
                if (field == "Quantity") {
                    eval(data.Quantity);
                }
                if (field == "DesignQuantity") {
                    eval(data.DesignQuantity);
                }
            } catch (e) {
                layer.msg('数据填写格式有误', { icon: 2, time: 1500 });
                table.reload('WX_MaterialFeeTable');
                return;
            }
            $.ajax({
                type: "POST",
                url: "/Estimate/PEMaterialFeeUpdate",
                async: false,
                contentType: "application/json",
                dataType: "json",
                data: JSON.stringify(obj.data),
                success: function (msg) {
                    if (msg.result == "OK") {
                        layer.msg('修改成功', { icon: 6, time: 1000 });
                        table.reload('WX_MaterialFeeTable');
                    } else {
                        alert(msg.result);
                    }
                }
            });
        });

        table.on("tool(WX_MaterialFeeTablefilter)", function (obj) {
            var data = obj.data; //获得当前行数据
            var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
            var tr = obj.tr; //获得当前行 tr 的DOM对象

            if (layEvent == 'TableDel') {
                layer.confirm('<span class="layui-icon"></span>' + '是否真的要删除“' + data.EquipName + " " + data.Specification + '？', {
                    btn: ['确定删除', '取消'] //可以无限个按钮
                    , btn1: function (index) {
                        //按钮【删除】的回调
                        $.ajax({
                            type: "POST",
                            url: "/Estimate/PEMaterialFeeDel",
                            async: false,
                            data: { "PEMaterialPriceId": data.PEMaterialPriceId },
                            success: function (msg) {
                                if (msg.result == "OK") {
                                    layer.msg('删除成功"', { icon: 6, time: 1000 });
                                    table.reload("WX_MaterialFeeTable", {

                                    });
                                } else {
                                    alert("删除失败");
                                }
                            }
                        });
                        layer.close(index);
                    }
                    , btn2: function (index) {
                        //按钮【按钮三】的回调
                        //alert("2");
                    }
                });
            }
        });


        //////////////////////////////////////////////////////////////////////
        //开闭所部分
        table.render({
            elem: '#KBS_MaterialFeeTable'
            , method: 'post'
            //, height: 315
            , url: '/Estimate/PEMaterialTableRequest' //数据接口
            , page: true //开启分页
            , where: { SortName: "PEMaterialPriceId", Order: "Desc", Condition: " and TabType='KBS' and IsDelete<>1 and PreEstId=" + PreEstId + " and PreEstType='" + PreEstType + "'" }
            , limits: [10, 50, 100]
            , cols: [[ //表头
                { field: 'Code', title: '编号', width: 100 }
                , { field: 'EquipName', title: '名称', width: 250, edit: 'text'}
                , { field: 'Specification', title: '规格型号', width: 250, edit: 'text'}
                , { field: 'Unit', title: '单位', width: 70 }
                , { field: 'DesignQuantity', title: '图纸量', width: 80, edit: 'text' }
                , { field: 'Quantity', title: '数量', width: 80, edit: 'text' }
                , { field: 'QuoteMoney', title: '单价', width: 100, edit: 'text' }
                , {
                    title: '合价', width: 120,
                    templet: function (d) {
                        return (d.Quantity * d.QuoteMoney).toFixed(2);
                    }
                }
                , {
                    title: '单价对比', width: 100,
                    templet: function (d) {
                        if (d.NormalPrice == null) {
                            return '无对比';
                        }
                        else if (d.QuoteMoney > d.NormalPrice) {
                            return '<i class="layui-icon layui-icon-up" style="font-size:16px;color:#FF0000;"></i>' + (d.QuoteMoney - d.NormalPrice).toFixed(0);
                        }
                        else if (d.QuoteMoney < d.NormalPrice) {
                            return '<i class="layui-icon layui-icon-down" style="font-size: 16px; color: #238E23;"></i>' + (d.NormalPrice - d.QuoteMoney).toFixed(0);
                        }
                        else {
                            return '--';
                        }
                    }
                }
                , {
                    field: "operation", title: "操作", width: 80, fixed: 'right',
                    templet: function (d) {
                        return '<a class="layui-btn layui-btn-xs" lay-event="TableDel">删除</a>';
                    }
                }

            ]]
        });

        table.on('edit(KBS_MaterialFeeTablefilter)', function (obj) { //注：edit是固定事件名，test是table原始容器的属性 lay-filter="对应的值"
            //console.log(obj.value); //得到修改后的值
            //console.log(obj.field); //当前编辑的字段名
            //console.log(obj.data); //所在行的所有相关数据  
            var data = obj.data;
            var field = obj.field;
            try {
                if (field == "QuoteMoney") {
                    eval(data.QuoteMoney);
                }
                if (field == "Quantity") {
                    eval(data.Quantity);
                }
                if (field == "DesignQuantity") {
                    eval(data.DesignQuantity);
                }
            } catch (e) {
                layer.msg('数据填写格式有误', { icon: 2, time: 1500 });
                table.reload('KBS_MaterialFeeTable');
                return;
            }
            $.ajax({
                type: "POST",
                url: "/Estimate/PEMaterialFeeUpdate",
                async: false,
                contentType: "application/json",
                dataType: "json",
                data: JSON.stringify(obj.data),
                success: function (msg) {
                    if (msg.result == "OK") {
                        layer.msg('修改成功', { icon: 6, time: 1000 });
                        table.reload('KBS_MaterialFeeTable');
                    } else {
                        alert(msg.result);
                    }
                }
            });
        });

        table.on("tool(KBS_MaterialFeeTablefilter)", function (obj) {
            var data = obj.data; //获得当前行数据
            var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
            var tr = obj.tr; //获得当前行 tr 的DOM对象

            if (layEvent == 'TableDel') {
                layer.confirm('<span class="layui-icon"></span>' + '是否真的要删除“' + data.EquipName + " " + data.Specification + '？', {
                    btn: ['确定删除', '取消'] //可以无限个按钮
                    , btn1: function (index) {
                        //按钮【删除】的回调
                        $.ajax({
                            type: "POST",
                            url: "/Estimate/PEMaterialFeeDel",
                            async: false,
                            data: { "PEMaterialPriceId": data.PEMaterialPriceId },
                            success: function (msg) {
                                if (msg.result == "OK") {
                                    layer.msg('删除成功"', { icon: 6, time: 1000 });
                                    table.reload("KBS_MaterialFeeTable", {

                                    });
                                } else {
                                    alert("删除失败");
                                }
                            }
                        });
                        layer.close(index);
                    }
                    , btn2: function (index) {
                        //按钮【按钮三】的回调
                        //alert("2");
                    }
                });
            }
        });

        ////////////////////////////////////////////////////////////////////
        //公配部分
        table.render({
            elem: '#GP_MaterialFeeTable'
            , method: 'post'
            //, height: 315
            , url: '/Estimate/PEMaterialTableRequest' //数据接口
            , page: true //开启分页
            , where: { SortName: "PEMaterialPriceId", Order: "Desc", Condition: " and TabType='GP' and IsDelete<>1 and PreEstId=" + PreEstId + " and PreEstType='" + PreEstType + "'" }
            , limits: [10, 50, 100]
            , cols: [[ //表头
                { field: 'Code', title: '编号', width: 100 }
                , { field: 'EquipName', title: '名称', width: 250, edit: 'text'}
                , { field: 'Specification', title: '规格型号', width: 250, edit: 'text'}
                , { field: 'Unit', title: '单位', width: 70 }
                , { field: 'DesignQuantity', title: '图纸量', width: 80, edit: 'text' }
                , { field: 'Quantity', title: '数量', width: 80, edit: 'text' }
                , { field: 'QuoteMoney', title: '单价', width: 100, edit: 'text' }
                , {
                    title: '合价', width: 120,
                    templet: function (d) {
                        return (d.Quantity * d.QuoteMoney).toFixed(2);
                    }
                }
                , {
                    title: '单价对比', width: 100,
                    templet: function (d) {
                        if (d.NormalPrice == null) {
                            return '无对比';
                        }
                        else if (d.QuoteMoney > d.NormalPrice) {
                            return '<i class="layui-icon layui-icon-up" style="font-size:16px;color:#FF0000;"></i>' + (d.QuoteMoney - d.NormalPrice).toFixed(0);
                        }
                        else if (d.QuoteMoney < d.NormalPrice) {
                            return '<i class="layui-icon layui-icon-down" style="font-size: 16px; color: #238E23;"></i>' + (d.NormalPrice - d.QuoteMoney).toFixed(0);
                        }
                        else {
                            return '--';
                        }
                    }
                }
                , {
                    field: "operation", title: "操作", width: 80, fixed: 'right',
                    templet: function (d) {
                        return '<a class="layui-btn layui-btn-xs" lay-event="TableDel">删除</a>';
                    }
                }

            ]]
        });

        table.on('edit(GP_MaterialFeeTablefilter)', function (obj) { //注：edit是固定事件名，test是table原始容器的属性 lay-filter="对应的值"
            //console.log(obj.value); //得到修改后的值
            //console.log(obj.field); //当前编辑的字段名
            //console.log(obj.data); //所在行的所有相关数据  
            var data = obj.data;
            var field = obj.field;
            try {
                if (field == "QuoteMoney") {
                    eval(data.QuoteMoney);
                }
                if (field == "Quantity") {
                    eval(data.Quantity);
                }
                if (field == "DesignQuantity") {
                    eval(data.DesignQuantity);
                }
            } catch (e) {
                layer.msg('数据填写格式有误', { icon: 2, time: 1500 });
                table.reload('GP_MaterialFeeTable');
                return;
            }
            $.ajax({
                type: "POST",
                url: "/Estimate/PEMaterialFeeUpdate",
                async: false,
                contentType: "application/json",
                dataType: "json",
                data: JSON.stringify(obj.data),
                success: function (msg) {
                    if (msg.result == "OK") {
                        layer.msg('修改成功', { icon: 6, time: 1000 });
                        table.reload('GP_MaterialFeeTable');
                    } else {
                        alert(msg.result);
                    }
                }
            });
        });

        table.on("tool(GP_MaterialFeeTablefilter)", function (obj) {
            var data = obj.data; //获得当前行数据
            var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
            var tr = obj.tr; //获得当前行 tr 的DOM对象

            if (layEvent == 'TableDel') {
                layer.confirm('<span class="layui-icon"></span>' + '是否真的要删除“' + data.EquipName + " " + data.Specification + '？', {
                    btn: ['确定删除', '取消'] //可以无限个按钮
                    , btn1: function (index) {
                        //按钮【删除】的回调
                        $.ajax({
                            type: "POST",
                            url: "/Estimate/PEMaterialFeeDel",
                            async: false,
                            data: { "PEMaterialPriceId": data.PEMaterialPriceId },
                            success: function (msg) {
                                if (msg.result == "OK") {
                                    layer.msg('删除成功"', { icon: 6, time: 1000 });
                                    table.reload("GP_MaterialFeeTable", {

                                    });
                                } else {
                                    alert("删除失败");
                                }
                            }
                        });
                        layer.close(index);
                    }
                    , btn2: function (index) {
                        //按钮【按钮三】的回调
                        //alert("2");
                    }
                });
            }
        });
        ///////////////////////////////////////////////////////////////////////////////////////
        //专配部分
        table.render({
            elem: '#ZP_MaterialFeeTable'
            , method: 'post'
            //, height: 315
            , url: '/Estimate/PEMaterialTableRequest' //数据接口
            , page: true //开启分页
            , where: { SortName: "PEMaterialPriceId", Order: "Desc", Condition: " and TabType='ZP' and IsDelete<>1 and PreEstId=" + PreEstId + " and PreEstType='" + PreEstType + "'" }
            , limits: [10, 50, 100]
            , cols: [[ //表头
                { field: 'Code', title: '编号', width: 100 }
                , { field: 'EquipName', title: '名称', width: 250, edit: 'text'}
                , { field: 'Specification', title: '规格型号', width: 250, edit: 'text'}
                , { field: 'Unit', title: '单位', width: 70 }
                , { field: 'DesignQuantity', title: '图纸量', width: 80, edit: 'text' }
                , { field: 'Quantity', title: '数量', width: 80, edit: 'text' }
                , { field: 'QuoteMoney', title: '单价', width: 100, edit: 'text' }
                , {
                    title: '合价', width: 120,
                    templet: function (d) {
                        return (d.Quantity * d.QuoteMoney).toFixed(2);
                    }
                }
                , {
                    title: '单价对比', width: 100,
                    templet: function (d) {
                        if (d.NormalPrice == null) {
                            return '无对比';
                        }
                        else if (d.QuoteMoney > d.NormalPrice) {
                            return '<i class="layui-icon layui-icon-up" style="font-size:16px;color:#FF0000;"></i>' + (d.QuoteMoney - d.NormalPrice).toFixed(0);
                        }
                        else if (d.QuoteMoney < d.NormalPrice) {
                            return '<i class="layui-icon layui-icon-down" style="font-size: 16px; color: #238E23;"></i>' + (d.NormalPrice - d.QuoteMoney).toFixed(0);
                        }
                        else {
                            return '--';
                        }
                    }
                }
                , {
                    field: "operation", title: "操作", width: 80, fixed: 'right',
                    templet: function (d) {
                        return '<a class="layui-btn layui-btn-xs" lay-event="TableDel">删除</a>';
                    }
                }

            ]]
        });

        table.on('edit(ZP_MaterialFeeTablefilter)', function (obj) { //注：edit是固定事件名，test是table原始容器的属性 lay-filter="对应的值"
            //console.log(obj.value); //得到修改后的值
            //console.log(obj.field); //当前编辑的字段名
            //console.log(obj.data); //所在行的所有相关数据  
            var data = obj.data;
            var field = obj.field;
            try {
                if (field == "QuoteMoney") {
                    eval(data.QuoteMoney);
                }
                if (field == "Quantity") {
                    eval(data.Quantity);
                }
                if (field == "DesignQuantity") {
                    eval(data.DesignQuantity);
                }
            } catch (e) {
                layer.msg('数据填写格式有误', { icon: 2, time: 1500 });
                table.reload('ZP_MaterialFeeTable');
                return;
            }
            $.ajax({
                type: "POST",
                url: "/Estimate/PEMaterialFeeUpdate",
                async: false,
                contentType: "application/json",
                dataType: "json",
                data: JSON.stringify(obj.data),
                success: function (msg) {
                    if (msg.result == "OK") {
                        layer.msg('修改成功', { icon: 6, time: 1000 });
                        table.reload('ZP_MaterialFeeTable');
                    } else {
                        alert(msg.result);
                    }
                }
            });
        });

        table.on("tool(ZP_MaterialFeeTablefilter)", function (obj) {
            var data = obj.data; //获得当前行数据
            var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
            var tr = obj.tr; //获得当前行 tr 的DOM对象

            if (layEvent == 'TableDel') {
                layer.confirm('<span class="layui-icon"></span>' + '是否真的要删除“' + data.EquipName + " " + data.Specification + '？', {
                    btn: ['确定删除', '取消'] //可以无限个按钮
                    , btn1: function (index) {
                        //按钮【删除】的回调
                        $.ajax({
                            type: "POST",
                            url: "/Estimate/PEMaterialFeeDel",
                            async: false,
                            data: { "PEMaterialPriceId": data.PEMaterialPriceId },
                            success: function (msg) {
                                if (msg.result == "OK") {
                                    layer.msg('删除成功"', { icon: 6, time: 1000 });
                                    table.reload("ZP_MaterialFeeTable", {

                                    });
                                } else {
                                    alert("删除失败");
                                }
                            }
                        });
                        layer.close(index);
                    }
                    , btn2: function (index) {
                        //按钮【按钮三】的回调
                        //alert("2");
                    }
                });
            }
        });

        ///////////////////////////////////////////////////////////////////////////////////////
        //运行准备部分
        table.render({
            elem: '#YXZB_MaterialFeeTable'
            , method: 'post'
            //, height: 315
            , url: '/Estimate/PEMaterialTableRequest' //数据接口
            , page: true //开启分页
            , where: { SortName: "PEMaterialPriceId", Order: "Desc", Condition: " and TabType='YXZB' and IsDelete<>1 and PreEstId=" + PreEstId + " and PreEstType='" + PreEstType + "'" }
            , limits: [10, 50, 100]
            , cols: [[ //表头
                { field: 'Code', title: '编号', width: 100 }
                , { field: 'EquipName', title: '名称', width: 250, edit: 'text'}
                , { field: 'Specification', title: '规格型号', width: 250, edit: 'text'}
                , { field: 'Unit', title: '单位', width: 70 }
                , { field: 'Quantity', title: '数量', width: 80, edit: 'text' }
                , { field: 'QuoteMoney', title: '单价', width: 100, edit: 'text' }
                , {
                    title: '合价', width: 120,
                    templet: function (d) {
                        return (d.Quantity * d.QuoteMoney).toFixed(2);
                    }
                }
                , {
                    title: '单价对比', width: 100,
                    templet: function (d) {
                        if (d.NormalPrice == null) {
                            return '无对比';
                        }
                        else if (d.QuoteMoney > d.NormalPrice) {
                            return '<i class="layui-icon layui-icon-up" style="font-size:16px;color:#FF0000;"></i>' + (d.QuoteMoney - d.NormalPrice).toFixed(0);
                        }
                        else if (d.QuoteMoney < d.NormalPrice) {
                            return '<i class="layui-icon layui-icon-down" style="font-size: 16px; color: #238E23;"></i>' + (d.NormalPrice - d.QuoteMoney).toFixed(0);
                        }
                        else {
                            return '--';
                        }
                    }
                }
                , {
                    field: "operation", title: "操作", width: 80, fixed: 'right',
                    templet: function (d) {
                        return '<a class="layui-btn layui-btn-xs" lay-event="TableDel">删除</a>';
                    }
                }

            ]]
        });

        table.on('edit(YXZB_MaterialFeeTablefilter)', function (obj) { //注：edit是固定事件名，test是table原始容器的属性 lay-filter="对应的值"
            //console.log(obj.value); //得到修改后的值
            //console.log(obj.field); //当前编辑的字段名
            //console.log(obj.data); //所在行的所有相关数据  
            var data = obj.data;
            var field = obj.field;
            try {
                if (field == "QuoteMoney") {
                    eval(data.QuoteMoney);
                }
                if (field == "Quantity") {
                    eval(data.Quantity);
                }
            } catch (e) {
                layer.msg('数据填写格式有误', { icon: 2, time: 1500 });
                table.reload('YXZB_MaterialFeeTable');
                return;
            }
            $.ajax({
                type: "POST",
                url: "/Estimate/PEMaterialFeeUpdate",
                async: false,
                contentType: "application/json",
                dataType: "json",
                data: JSON.stringify(obj.data),
                success: function (msg) {
                    if (msg.result == "OK") {
                        layer.msg('修改成功', { icon: 6, time: 1000 });
                        table.reload('YXZB_MaterialFeeTable');
                    } else {
                        alert(msg.result);
                    }
                }
            });
        });

        table.on("tool(YXZB_MaterialFeeTablefilter)", function (obj) {
            var data = obj.data; //获得当前行数据
            var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
            var tr = obj.tr; //获得当前行 tr 的DOM对象

            if (layEvent == 'TableDel') {
                layer.confirm('<span class="layui-icon"></span>' + '是否真的要删除“' + data.EquipName + " " + data.Specification + '？', {
                    btn: ['确定删除', '取消'] //可以无限个按钮
                    , btn1: function (index) {
                        //按钮【删除】的回调
                        $.ajax({
                            type: "POST",
                            url: "/Estimate/PEMaterialFeeDel",
                            async: false,
                            data: { "PEMaterialPriceId": data.PEMaterialPriceId },
                            success: function (msg) {
                                if (msg.result == "OK") {
                                    layer.msg('删除成功"', { icon: 6, time: 1000 });
                                    table.reload("YXZB_MaterialFeeTable", {

                                    });
                                } else {
                                    alert("删除失败");
                                }
                            }
                        });
                        layer.close(index);
                    }
                    , btn2: function (index) {
                        //按钮【按钮三】的回调
                        //alert("2");
                    }
                });
            }
        });



    });

    //引用表搜索
    function QuoteGetKey() {
        if (event.keyCode == 13) {
            QuoteSearchFunc();
        }
    }
    function QuoteSearchFunc() {
        var text = $("#QuoteSearchText").val();
        if (text == "") {
            table.reload("QuoteMaterialFeeTable", {
                where: {
                    SortName: "EquipPriceId", Order: "Desc", Condition: " and IsDelete<>1"
                },
                page: {
                    curr: 1
                }
            });
        }
        else {
            table.reload("QuoteMaterialFeeTable", {
                where: {
                    SortName: "EquipPriceId", Order: "Desc", Condition: " and IsDelete<>1 and (EquipName like '%" + text + "%' or Specification like '%" + text + "%')"
                },
                page: {
                    curr: 1
                }
            });
        }
    }


    //数据表搜索回车键
    function getKey() {
        if (event.keyCode == 13) {
            SearchFunc();
        }
    }
    function SearchFunc() {
        var text = $("#SearchText").val();
        if (text == "") {
            if (TabIndex == 0) {
                table.reload("WX_MaterialFeeTable", {
                    where: {
                        SortName: "PEMaterialPriceId", Order: "Desc", Condition: " and TabType='WX' and IsDelete<>1 and PreEstId=" + PreEstId + " and PreEstType='" + PreEstType + "'"
                    }
                });
            }
            else if (TabIndex == 1) {
                table.reload("KBS_MaterialFeeTable", {
                    where: {
                        SortName: "PEMaterialPriceId", Order: "Desc", Condition: " and TabType='KBS' and IsDelete<>1 and PreEstId=" + PreEstId + " and PreEstType='" + PreEstType + "'"
                    }
                });
            }
            else if (TabIndex == 2) {
                table.reload("GP_MaterialFeeTable", {
                    where: {
                        SortName: "PEMaterialPriceId", Order: "Desc", Condition: " and TabType='GP' and IsDelete<>1 and PreEstId=" + PreEstId + " and PreEstType='" + PreEstType + "'"
                    }
                });
            }
            else if (TabIndex == 3) {
                table.reload("ZP_MaterialFeeTable", {
                    where: {
                        SortName: "PEMaterialPriceId", Order: "Desc", Condition: " and TabType='ZP' and IsDelete<>1 and PreEstId=" + PreEstId + " and PreEstType='" + PreEstType + "'"
                    }
                });
            }
            else if (TabIndex == 4) {
                table.reload("YXZB_MaterialFeeTable", {
                    where: {
                        SortName: "PEMaterialPriceId", Order: "Desc", Condition: " and TabType='YXZB' and IsDelete<>1 and PreEstId=" + PreEstId + " and PreEstType='" + PreEstType + "'"
                    }
                });
            }

        }
        else {
            if (TabIndex == 0) {
                table.reload("WX_MaterialFeeTable", {
                    where: {
                        SortName: "PEMaterialPriceId", Order: "Desc", Condition: " and TabType='WX' and IsDelete<>1 and (EquipName like '%" + text + "%' or Specification like '%" + text + "%')"
                    }
                });
            }
            else if (TabIndex == 1) {
                table.reload("KBS_MaterialFeeTable", {
                    where: {
                        SortName: "PEMaterialPriceId", Order: "Desc", Condition: " and TabType='KBS' and IsDelete<>1 and (EquipName like '%" + text + "%' or Specification like '%" + text + "%')"
                    }
                });
            }
            else if (TabIndex == 2) {
                table.reload("GP_MaterialFeeTable", {
                    where: {
                        SortName: "PEMaterialPriceId", Order: "Desc", Condition: " and TabType='GP' and IsDelete<>1 and (EquipName like '%" + text + "%' or Specification like '%" + text + "%')"
                    }
                });
            }
            else if (TabIndex == 3) {
                table.reload("ZP_MaterialFeeTable", {
                    where: {
                        SortName: "PEMaterialPriceId", Order: "Desc", Condition: " and TabType='ZP' and IsDelete<>1 and (EquipName like '%" + text + "%' or Specification like '%" + text + "%')"
                    }
                });
            }
            else if (TabIndex == 4) {
                table.reload("YXZB_MaterialFeeTable", {
                    where: {
                        SortName: "PEMaterialPriceId", Order: "Desc", Condition: " and TabType='YXZB' and IsDelete<>1 and (EquipName like '%" + text + "%' or Specification like '%" + text + "%')"
                    }
                });
            }

        }
    }






</script>